<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>文章</title>
		<link rel="stylesheet" href="__PEAR__/component/pear/css/pear.css" />
		<link rel="stylesheet" href="__PEAR__/admin/css/other/department.css"/>
	</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">标题</label>
							<div class="layui-input-inline">
								<input type="text" name="branch_no" placeholder="" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">发布时间</label>
							<div class="layui-input-inline" style="width:120px;">
								<input type="text" name="startTime" id="startTime" autocomplete="off" placeholder="开始时间" class="layui-input" value="" style="width:120px;">
							</div>
							<div class="layui-input-inline" style="width:120px;">	
								<input type="text" name="endTime" id="endTime" autocomplete="off" placeholder="结束时间" class="layui-input" style="width:120px;">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">审核状态</label>
							<div class="layui-input-inline">
							<select name="approve" lay-verify="">
								<option value="">全部</option>
								<option value="1">已审核</option>
								<option value="-1">未审核</option>
							</select>
							</div>
						</div>
						
						<div class="layui-form-item layui-inline">
							<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="channel-query">
								<i class="layui-icon layui-icon-search"></i>
								查询
							</button>
							<button type="reset" class="pear-btn pear-btn-md">
								<i class="layui-icon layui-icon-refresh"></i>
								重置
							</button>
						</div>
					</div>
				</form>
			</div>
		</div>
		<div class="layui-row layui-col-space15">
			<div class="layui-col-md3">
				<div class="layui-card">
					<div class="layui-card-body">
						<div id="channelTreeContent" style="overflow: auto">
							<ul id="channelTree" class="dtree organizationTree"></ul>
						</div>
					</div>
				</div>
			</div>
			<div class="layui-col-md9">
				<div class="layui-card">
					<div class="layui-card-body">
						<table id="channel-table" lay-filter="channel-table"></table>
					</div>
				</div>
			</div>
		</div>

		<script type="text/html" id="channel-toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
			<i class="layui-icon layui-icon-add-1"></i>
			新增
		</button>
		<button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
			<i class="layui-icon layui-icon-delete"></i>
			删除
		</button>
	</script>

		<script type="text/html" id="channel-bar">
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i
				class="layui-icon layui-icon-delete"></i></button>
	    </script>
		<script src="__STATIC__/lib/layui/layui.js"></script>
		<script src="__PEAR__/component/pear/pear.js"></script>
		<script src="__STATIC__/js/admin/common.js"></script>
		<script>
			var _parent="";
			
			layui.use(['table', 'form', 'jquery', 'dtree','common','laydate'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let dtree = layui.dtree;
				let common = layui.common;
				let laydate = layui.laydate;
				
			  	laydate.render({
					    elem: '#endTime'
			  	});
			  	
			  	laydate.render({
				    elem: '#startTime'
		  		});
				
				_parent='';
				
				let MODULE_PATH = "/{$MODULE_NAME}/portal/";
				let DATA_PATH = MODULE_PATH+"Jsondata/";
				let TREE_PATH=DATA_PATH+"GetChannels";

				let cols = [
					[	{
							type: 'checkbox'
						},
						{
							title: 'ID',
							field: 'content_id',
							align: 'center'
						},
						{
							title: '标题',
							field: 'title',
							align: 'center'
						},
						{
							title: '栏目名称',
							field: 'channel_name',
							align: 'center'
						},
						{
							title: '内容类型',
							field: 'type_name',
							align: 'center'
						},
						{
							title: '发布者',
							field: 'user_id',
							align: 'center'
						},
						{
							title: '发布时间',
							field: 'release_date',
							align: 'center'
						},
						{
							title: '状态',
							field: 'status',
							align: 'center'
						},
						{
							title: '操作',
							toolbar: '#channel-bar',
							align: 'center',
							width: 130
						}
					]
				]
				
				var treeHeight=parent.globalHeight-25;
				var DTree = dtree.render({
					elem: "#channelTree",
					height:treeHeight,
					initLevel: "2", //默认展开层级为1
					line: true, // 有线树
					ficon: ["1", "-1"], // 设定一级图标样式。0表示方形加减图标，8表示小圆点图标
					icon: ["0", "0"], // 设定二级图标样式。0表示文件夹图标，5表示叶子图标
					method: 'get',
					url: TREE_PATH
				});

				table.render({
					elem: '#channel-table',
					url: DATA_PATH+'GetPortalContentByChannel',
					height: 'full-50',
					page: true,
					cols: cols,
					skin: 'line',
					toolbar: '#channel-toolbar',
					defaultToolbar: []
				});

				// 绑定节点点击事件
				dtree.on("node(channelTree)", function(obj) {
					_parent=obj.param.nodeId;
					
					//console.log(obj.param.nodeId);
					
					//layer.msg("正在获取数据");
					table.reload("channel-table",{
							where:{'channelno':_parent}
					});
					
				});

				table.on('tool(channel-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					}
				});

				table.on('toolbar(channel-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					} else if (obj.event === 'batchRemove') {
						window.batchRemove(obj);
					}
				});

				form.on('submit(channel-query)', function(data) {
					table.reload('channel-table', {
						where: data.field,
						page:{curr:1}
					})
					return false;
				});

				window.add = function() {
					if(_parent==''||_parent==0){
						layer.msg("请从左边选择栏目");
						return;
					}
					layer.open({
						type: 2,
						title: '新增文章',
						shade: 0.1,
						area: ['1000px', '600px'],
						content: MODULE_PATH + 'content/view/channelno/'+_parent+'/option/add.html'
					});
				}
				
				window.edit = function(obj) {
					
					var _parent=obj.data.channel_id;
					var _id=obj.data.content_id;
					
					layer.open({
						type: 2,
						title: '修改文章',
						shade: 0.1,
						area: ['1000px', '600px'],
						content: MODULE_PATH + 'content/view/channelno/'+_parent+'/contentno/'+_id+'/option/update.html'
					});
				}

				window.remove = function(obj) {
					
					layer.confirm('确定要删除该文章?', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: MODULE_PATH + "content/delete/channelid/" + obj.data['channel_id']+"/contentid/"+obj.data['content_id'],
							dataType: 'json',
							type: 'delete',
							success: function(result) {
								layer.close(loading);
								if (result.code) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										obj.del();
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}
				
				window.batchRemove = function(obj) {
					
					var checkIds = common.checkField(obj,'content_id');
					
					if (checkIds === "") {
						layer.msg("未选中数据", {
							icon: 3,
							time: 1000
						});
						return false;
					}
					
					layer.confirm('确定要删除这些文章?', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: MODULE_PATH + "content/batchDelete/contentid/" + checkIds,
							dataType: 'json',
							type: 'delete',
							success: function(result) {
								layer.close(loading);
								if (result.code) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										table.reload('channel-table');
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}

				window.refresh = function(param) {
					table.reload('channel-table');
				}
				
				//$(window).resize(function(){
					//var treeHeight=getParentWinHeight($)-25;
					//rebuildTree(dtree,"channelTree",TREE_PATH,treeHeight);
				//})
				
			})
		</script>
	</body>
</html>
